﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KMF.Common;

namespace KMF.DAL
{
    public partial class DiscountDb
    {
        /// <summary>
        /// Thêm chiết khấu mới
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddDiscount(DiscountDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Discount_Add");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Name", model.Name));
                command.Parameters.Add(dao.CreateParameter("@DiscountTypeId", model.DiscountTypeId));
                command.Parameters.Add(dao.CreateParameter("@UsePercentage", model.UsePercentage));
                command.Parameters.Add(dao.CreateParameter("@DiscountPercentage", model.DiscountPercentage));
                command.Parameters.Add(dao.CreateParameter("@DiscountAmount", model.DiscountAmount));
                command.Parameters.Add(dao.CreateParameter("@StartDate", model.StartDate));
                command.Parameters.Add(dao.CreateParameter("@EndDate", model.EndDate));
                command.Parameters.Add(dao.CreateParameter("@RequiresCouponCode", model.RequiresCouponCode));
                command.Parameters.Add(dao.CreateParameter("@CouponCode", model.CouponCode));
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                command.Parameters.Add(dao.CreateParameter("@DiscountLimitationId", model.DiscountLimitationId));
                command.Parameters.Add(dao.CreateParameter("@LimitationTimes", model.LimitationTimes));
                int result = (int)command.ExecuteScalar();
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }//End method

        /// <summary>
        /// Lấy danh sách Discount
        /// </summary>
        /// <param name="page"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="totalPage"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static List<ViewDiscountPagingDto> GetAllDiscount(int page, int itemsPerPage, ref int totalPage, ref int totalRecord)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Discount_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));

                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                SqlParameter itemTotalRecord = command.Parameters.Add("@TotalRecord", SqlDbType.Int);
                itemTotalRecord.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ViewDiscountPagingDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                totalRecord = int.Parse(itemTotalRecord.Value.ToString() == "" ? "0" : itemTotalRecord.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xóa chiết khấu
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int DeleteDiscount(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Discount_Delete");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@DiscountId", id));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Thêm bản ghi vào bảng DiscountRequirement
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddDiscountRequirement(DiscountRequirementDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_DiscountRequirement_Add");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@DiscountId", model.DiscountId));
                command.Parameters.Add(dao.CreateParameter("@DiscountRequirementRuleSystemName", model.DiscountRequirementRuleSystemName));
                return (int)command.ExecuteScalar();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về danh sách chiến dịch chiết khấu đang hoạt động, áp dụng tới sản phẩm
        /// </summary>
        /// <returns></returns>
        public static List<DiscountDto> GetDiscountApplyProduct()
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Discount_GetDiscountApplyProduct");
            try
            {
                return dao.GetDTOList<DiscountDto>(ref command);
            }
            finally
            {
                command.Connection.Close(); ;
            }
        }

        /// <summary>
        /// Lấy GiftId theo ProductId
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static int GetDiscountIdByProductId(string productId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_DiscountApplyToProduct_GetGiftIdByProductId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                return dao.GetSingleInt32(command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về chiến dịch khuyến mại theo danh mục
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public static int GetDiscountIdByCategoryId(string categoryId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_DiscountApplyToProduct_GetGiftIdByCategoryId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@CategoryId", categoryId));
                return dao.GetSingleInt32(command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xoá thông tin chiết khấu áp dụng tới sản phẩm từ bảng DiscountApplyToProduct
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static int DeleteDiscountApplyProduct(string productId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_DiscountApplyToProduct_DeleteByProductId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xóa chiết khấu áp dụng tới danh mục trong bảng DiscountApplyToCategory
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public static int DeleteDiscountApplyCategory(string categoryId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_DiscountApplyToCategory_DeleteByCategoryId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@CategoryId", categoryId));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về danh sách chiết khấu áp dụng tới danh mục
        /// </summary>
        /// <returns></returns>
        public static List<DiscountDto> GetDiscountApplyCategory()
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Discount_GetDiscountApplyCategory");
            try
            {
                return dao.GetDTOList<DiscountDto>(ref command);
            }
            finally
            {
                command.Connection.Close(); ;
            }
        }
    }
}
